#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require

#include "../../../../core/shader_std.h"
#include "../../../../core/shader_built_in_functions.h"
#include "../../../../core/shader_color.h"


layout(location = 0) in vec2 inPoint;
layout(location = 1) in vec2 inWdigths;
layout(location = 2) in vec2 inNormal;

layout(location = 0) out vec4 vColor;


layout(push_constant, scalar) uniform DRAWPROP_push_constants {
	S_2DConvexElement_PanelAttr const_push;
};

layout(std140, set = 0, binding = DEF_BINDING_PROJECT) uniform U_Matrix0 {
	mat4 mat_ProjecView;
};

layout(std140, set = 1, binding = 1, scalar) readonly buffer U_Matrix1 {
	mat3 tranformG[];
};
layout(std140, set = 1, binding = 2, scalar) readonly buffer U_Matrix2 {
	mat3 tranformL[];
};

layout(std140, set = 2, binding = 1, scalar) readonly buffer U_Matrix3 {
	uint elementColor[];
};

layout(std140, set = 3, binding = 2, scalar) readonly buffer U_Matrix4 {
	S_2DConvexRect ConvexRect[];
};


out gl_PerVertex {
	vec4	gl_Position;
};


//vec2 pos[4] = {vec2(10.0, 10.0), vec2(100.0, 10.0), vec2(10.0, 100.0), vec2(100.0, 100.0)};

void main(void){
	mat3 mat = tranform[const_push.m_Offset_Instance + gl_InstanceIndex];
	S_2DConvexRect rect = ConvexRect[const_push.m_Offset_Rect + gl_DrawID];
	
	vec2 radius = clamp(rect.size - rect.radius, vec2(0), rect.radius);
	vec2 vert = inPoint * radius;
	
	vert += max(vec2(0), rect.size - radius) * inWdigths;
	//vert.x -= inWdigths.x * 2;
	
	vert += rect.offset;
	vec4 v = vec4(mat * vec3(vert, 1.0), 1.0);
	//vec4 v = vec4(vec3(vert, 0.0), 1.0);

	//mat4 project;
	//project[0] = vec4(0.00156250002, 0.00000000, 0.00000000, 0.00000000);
	//project[1] = vec4(0.00000000, 0.00277777785, 0.00000000, 0.00000000);
	//project[2] = vec4(0.00000000, 0.00000000, -0.00100000005, 0.00000000);
	//project[3] = vec4(-1.00000000, -1.00000000, -0.00000000, 1.00000000);

	//gl_Position = project * v;
	gl_Position = mat_ProjecView * v;
	vColor = f_glsl_buildColor(rect.color_type.x);
}



